using System;
using UnityEngine;
using Utils;
using Random = UnityEngine.Random;

public class SmallGun : Weapon
{
    public Animator animator;
    public GameObject bulletPrefab;
    public float bulletSpeed;
    public Transform pos;
    public float cd = 1;
    public float timing;

    private void Start()
    {
        effectAudio.clip = GameController.Instance().audioCtr.GetMusic("Gun/gun_03");
        // mp = 0;
        animator = GetComponent<Animator>();
        pos = UITool.GetComponent<Transform>("Pos", gameObject);
        // bulletPrefab = pos.GetComponentInChildren<Bullet>().gameObject;
        bulletPrefab.SetActive(false);
        self = PrefabRoot.SmallGun;
    }
    
    private void Update()
    {
        CheckKeysState();
#if UNITY_STANDALONE_WIN
        ChangeOrientation();
#endif
        EnemyWeaponLookAtPlayer();
    }

    public override void AttackButtonPress()
    {
        if (Time.timeScale != 0 && Time.time - timing >= cd)
        {
            timing = Time.time;
            // GameObject bullet = Instantiate(bulletPrefab, pos.position,
            //     pos.rotation * Quaternion.AngleAxis(Random.Range(0, shake), Vector3.forward));
            GameObject bulletGo = GameController.Instance().ObjectCtr.SimpleBullet();
            bulletGo.transform.position = pos.position;
            bulletGo.transform.rotation = pos.rotation * Quaternion.AngleAxis(Random.Range(0, shake), Vector3.forward);
            bulletGo.SetActive(true);
            bulletGo.tag = transform.parent.tag;
            bulletGo.transform.parent = GameController.Instance().gameCore.transform;
            bulletGo.GetComponent<Bullet>().InitData(attack, bulletSpeed);

            //射击动画效果
            animator.Play("SmallGun_Attack");
            effectAudio.Play();
        }
    }

    private void EnemyWeaponLookAtPlayer()
    {
        if (tag.Equals("Enemy"))
            UpdateLookAt(GameController.Instance().playerCtr.transform.position);
    }
    
}